/*
<Copyright (C) 2011 Dru Moore>

This file is part of demorecorder.

vlc-remote is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

demorecorder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with demorecorder.  If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 1.1
import com.meego 1.0
import "common"

Page {
    id: recorderPage
    tools: commonTools

    property int buttonWidth:  230
    property int buttonHeight:  230
    property int timeposition:  0
    property int timeduration:  0
    property int timerecorder:  0

    Component.onCompleted: {
        screen.allowedOrientations = Screen.Portrait;
        orientationLock = Screen.Portrait;
    }

    Connections {
        target: demoRecorderObject
        onPlaybackEnded: {
            if (timer.running) { timer.stop(); }
            demorecorder.playing = false;
        }
        onPipelineReady: {
            demorecorder.playng = false;
        }
    }

    Timer {
        id: timer
        interval: 100;
        repeat: true
        running: false
        triggeredOnStart: true

        onTriggered: {
            timeposition = demoRecorderObject.getPlayheadPosition();
            timeduration = demoRecorderObject.getPlayheadDuration();
            timerecorder = demoRecorderObject.getRecorderPosition();
        }
    }

    Column {
        width: recorderPage.width

        Row {
            id: timersGrid
            spacing: 12

            DigitalTimer {
                id: recorderTimer
                width: buttonWidth
                anchors.top: parent.top
                fontSize: 60
                fontSpacing: -8
                fontColor: "#660000"
                label: "R:"
                milliseconds: timerecorder
            }

            Column {
                spacing: 0

                DigitalTimer {
                    id: positionTimer
                    width: buttonWidth
                    anchors.top: parent.top
                    fontSize: 48
                    fontSpacing: -7
                    fontColor: "#006600"
                    label: "P:"
                    milliseconds: timeposition
                }
                DigitalTimer {
                    id: durationTimer
                    width: buttonWidth
                    anchors.top: positionTimer.bottom
                    fontSize: 36
                    fontSpacing: -5
                    fontColor: "#006600"
                    label: "D:"
                    milliseconds: timeduration
                }

            }

        }

        Rectangle {
            id: spacer
            height: 80
            width: parent.width
            color: "black"
            Text {
                id: projectName
                color: "#999999"
                font.pixelSize: 36
                //font.family: "monospace"
                height: parent.height
                width: parent.width
                horizontalAlignment: Text.AlignLeft
                maximumLineCount: 2
                wrapMode: "WrapAtWordBoundaryOrAnywhere"
                text: "Untitled Project click here to rename."
            }
        }


        Grid {
            id: controlGrid
            columns:  2
            rows: 3
            spacing: 20

            Button {
                id: btnRecordStop
                enabled: true
                iconSource: demorecorder.recording ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-stop-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-camera-ongoing-recording.png"
                width: buttonWidth
                height: buttonHeight
                onClicked: {
                    console.debug("btnRecord");
                    if (timer.running && !playing) { demoRecorderObject.stoprecording(); timer.stop(); timerecorder = 0; }
                    else if (!playing) {  demoRecorderObject.record(); timer.start(); }
                    else if (!recording) {  demoRecorderObject.record(); }
                    else {
                        demoRecorderObject.stoprecording();
                        timer.stop();
                    }
                    demorecorder.recording = !demorecorder.recording;
                }
            }
            Button {
                id: btnPlayPause
                enabled: !demorecorder.recording
                iconSource: demorecorder.playing ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-pause-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-play-white.png"
                width: buttonWidth
                height: buttonHeight
                onClicked: {
                    console.debug("btnPlay");
                    if (demorecorder.playing) {
                        demoRecorderObject.pause();
                        if (!recording) {
                            timer.stop();
                        }
                    }
                    else { demoRecorderObject.play(); timer.start(); }
                    demorecorder.playing = !demorecorder.playing;
                }
            }
            Button {
                id: btnRewind
                enabled: !demorecorder.recording && !demorecorder.playing
                iconSource: "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-backwards-white.png"
                width: buttonWidth
                height: buttonHeight / 2
                onClicked: {
                    console.debug("btnRewind: ");
                    demoRecorderObject.rewind();
                }
            }
            Button {
                id: btnAdvance
                enabled: !demorecorder.recording && !demorecorder.playing
                iconSource: "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-forward-white.png"
                width: buttonWidth
                height: buttonHeight / 2
                onClicked: {
                    console.debug("btnAdvance: ");
                    demoRecorderObject.advance();
                }
            }
            Button {
                id: btnInputOptions
                enabled: !demorecorder.recording && !demorecorder.playing
                iconSource: "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-content-podcast-audio-inverse.png"
                width: buttonWidth
                height: buttonHeight / 2
                onClicked: {
                    var path = "/home/user/demorecorder/projects/default/";
                    console.debug("btnInput: " + path);
                    demoRecorderObject.setProjectFolder(path);
                }
            }
            Button {
                id: btnMixerVolume
                enabled:  true
                iconSource: "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-volume-white-selected.png"
                width: buttonWidth
                height: buttonHeight / 2
                onClicked: {
                    console.debug("btnVolume");
                }
            }

        }

    }


}
